﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace BSC_OTC_Management.Forms.TKTienKH
{
    public partial class Frm_RutTien : Form
    {

        #region Khai bao bien
        Code.CL_Ultility _cl = new Code.CL_Ultility();
        string matk = "";
        public int _duyet = (int)Code.StaticVariable.approveCase.ChuaDuyet;
        #endregion

        public Frm_RutTien()
        {
            InitializeComponent();
            this.btt_Confr.Visible = true;
        }

        //truyen thong tin xem lai tu form duyet giao dich
        public Frm_RutTien(DataTable _dt, string _function)
        {
            try
            {
                InitializeComponent();

                //read only
                splitContainer2.Panel1.Enabled = false;
                btt_Confr.Text = "Duyệt";
                btt_Confr.Tag = Code.StaticVariable.function_Approve;
                //showdata
                this.matk = _dt.Rows[0]["matk"].ToString();
                txt_sl.Text = _cl.NumberFormat(_dt.Rows[0]["soluong"].ToString());
                txt_diengiai.Text = _dt.Rows[0]["notes"].ToString();
                ShowData(matk);
                //06-08-2017:DATNT ẩn hiện nút duyệt theo chức năng
                if (_function == Code.StaticVariable.function_View)
                    this.btt_Confr.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi hiển thị dữ liệu",Code.StaticVariable.messeseboxLuuY);
            }
        }

        private void btt_Confr_Click(object sender, EventArgs e)
        {
            try
            {
                if (btt_Confr.Tag == Code.StaticVariable.function_Approve)//duyệt dl
                {
                    this._duyet = (int)Code.StaticVariable.approveCase.DaDuyet;
                    this.Close();
                }
                else
                {
                    if (txt_matk.Text.Length > 0)
                    {
                        //duyệt giao dịch trước khi thực thi
                        // tao bang chua row can insert
                        DataTable tableRow = new DataTable("BSC_TBL_TLLOGALL");

                        // tao cac cot 
                        tableRow.Columns.Add("NGAYTAOGD");
                        tableRow.Columns.Add("MAKH");
                        tableRow.Columns.Add("MASO");
                        tableRow.Columns.Add("TENNGHIEPVU");
                        tableRow.Columns.Add("SOTIEN_SOLUONG");
                        tableRow.Columns.Add("DUYET");
                        tableRow.Columns.Add("NGUOIDUYET");
                        tableRow.Columns.Add("MAGIAODICH");
                        tableRow.Columns.Add("DIENGIAI");
                        tableRow.Columns.Add("NGAYDUYETGD");
                        tableRow.Columns.Add("GIOTAOGD");
                        tableRow.Columns.Add("NGUOITAOGD");

                        // tao 1 row chua du lieu
                        DataRow rowData = tableRow.NewRow();
                        rowData["NGAYTAOGD"] = DateTime.ParseExact(dtp_ngaychungtu.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
                        rowData["MAKH"] = txt_makh.Text;
                        rowData["MASO"] = txt_matk.Text;
                        rowData["TENNGHIEPVU"] = this.Text;
                        rowData["SOTIEN_SOLUONG"] = Convert.ToDouble(txt_sl.Text);
                        rowData["DUYET"] = "0";
                        rowData["MAGIAODICH"] = "603";
                        rowData["DIENGIAI"] = txt_diengiai.Text;
                        rowData["GIOTAOGD"] = _cl.Current_DATE("HH:mm:ss");
                        rowData["NGUOITAOGD"] = Code.StaticVariable.userID;
                        tableRow.Rows.Add(rowData);

                        // tao 1 row chua kieu du lieu
                        DataRow rowDataType = tableRow.NewRow();
                        rowDataType["NGAYTAOGD"] = "D";
                        rowDataType["MAKH"] = "T";
                        rowDataType["MASO"] = "T";
                        rowDataType["TENNGHIEPVU"] = "T";
                        rowDataType["SOTIEN_SOLUONG"] = "T";
                        rowDataType["DUYET"] = "T";
                        rowDataType["NGUOIDUYET"] = "T";
                        rowDataType["MAGIAODICH"] = "T";
                        rowDataType["DIENGIAI"] = "T";
                        rowDataType["NGAYDUYETGD"] = "D";
                        rowDataType["GIOTAOGD"] = "T";
                        rowDataType["NGUOITAOGD"] = "T";
                        tableRow.Rows.Add(rowDataType);

                        string AUTOID = _cl._services.AddTransData(tableRow);

                        //lưu thông tin vào bảng tạm
                        DataTable temptable = new DataTable("BSC_TBL_RUTTIEN");

                        // tao cac cot 
                        temptable.Columns.Add("MATK");
                        temptable.Columns.Add("MAKH");
                        temptable.Columns.Add("SOLUONG");
                        temptable.Columns.Add("NOTES");
                        temptable.Columns.Add("IDGD");

                        // tao 1 row chua du lieu
                        DataRow row = temptable.NewRow();
                        row["MATK"] = txt_matk.Text;
                        row["MAKH"] = txt_makh.Text;
                        row["SOLUONG"] = Convert.ToDouble(txt_sl.Text);
                        row["NOTES"] = txt_diengiai.Text;
                        row["IDGD"] = AUTOID;
                        temptable.Rows.Add(row);

                        // tao 1 row chua kieu du lieu
                        DataRow rowType = temptable.NewRow();
                        rowType["MATK"] = "T";
                        rowType["MAKH"] = "T";
                        rowType["SOLUONG"] = "T";
                        rowType["NOTES"] = "T";
                        rowType["IDGD"] = "T";
                        temptable.Rows.Add(rowType);

                        string result = _cl._services.AddNewRow("BSC_TBL_RUTTIEN", temptable);
                        if (result == Code.StaticVariable.messeseboxThemmoiThanhCong)
                        {
                            MessageBox.Show(Code.StaticVariable.messeseboxChuaDuyet, Code.StaticVariable.messeseboxLuuY);
                            _cl.ClearAllText(this);
                        }
                        else
                            MessageBox.Show(result, Code.StaticVariable.messeseboxLuuY);

                    }
                    else
                        MessageBox.Show(Code.StaticVariable.messeseboxKoDcBoTrong, Code.StaticVariable.messeseboxLuuY);
                }
                /*DataTable table = new DataTable("noname");
                table.Columns.Add("matk", typeof(String));
                table.Columns.Add("makh", typeof(String));
                table.Columns.Add("soluong", typeof(String));
                table.Columns.Add("notes", typeof(String));
                table.Rows.Add(textBox1.Text, textBox2.Text, textBox7.Text, textBox8.Text);
                table.Rows.Add("T", "T", "T", "T");
                string store = "bsc_602.Rut_tien_mat";
                string _result = _cl._services.Excute_Store_Phong_toa_TP(store, table);
                if (_result == "0")
                {
                    MessageBox.Show(Code.StaticVariable.messeseboxThanhCong);
                    _cl.ClearAllText(this);
                }
                else
                    MessageBox.Show(Code.StaticVariable.messeseboxThatBai);*/
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void btt_Cancel_Click(object sender, EventArgs e)
        {
            _duyet = (int)Code.StaticVariable.approveCase.ChuaDuyet;
            this.Close();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                try
                {
                    //chuyển sang Form View để tìm kiếm Sổ trái chủ
                    Forms.FrmView _FrmView = new Forms.FrmView("BSC_TBL_TAIKHOAN", "Thông tin Tài khoản tiền", true);
                    _FrmView.ShowDialog();
                    //lấy về dữ liệu DataTable formView Sotraichu
                    DataTable dt = Code.StaticVariable._tbl;
                    String matk = "";
                    if (dt.Columns.Count >= 5 && dt.Rows.Count >= 1)
                    {
                        matk = dt.Rows[0]["matk"].ToString();
                    }
                    Code.StaticVariable._tbl.Clear();
                    ShowData(matk);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void textBox7_Leave(object sender, EventArgs e)
        {
            try
            {
                if (txt_sl.Text.Length > 0)
                {
                    double temp = Convert.ToDouble(txt_sl.Text);
                    txt_sl.Text = String.Format("{0:#,##0.###}", temp);
                }
                else
                {
                    MessageBox.Show(Code.StaticVariable.messeseboxKoDcBoTrong, Code.StaticVariable.messeseboxLuuY);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.ToString());
            }
        }

        public void ShowData(string matk)
        {
            String sql = "Select tk.matk, tk.makh, kh.hoten, kh.cmt, kh.gpkd, kh.dienthoai, ac.cdcontent loaikh, tk.sodu "
                                        + "from bsc_tbl_khachhang kh, bsc_tbl_taikhoan tk "
                                        + "inner join allcode ac on ac.cdname='LOAIKH' and ac.cdtype= 'BSC_TBL_KHACHHANG' "
                                        + "where tk.makh = kh.makh and tk.matk = '" + matk + "'"
                                        + "and kh.loaikh = ac.cdval";

            DataTable result = _cl._services.GetDatatable_BySQL(sql);
            if (result.Columns.Count >= 6 && result.Rows.Count >= 1)
            {
                txt_matk.Text = result.Rows[0]["matk"].ToString();
                txt_makh.Text = result.Rows[0]["makh"].ToString();
                txt_hoten.Text = result.Rows[0]["hoten"].ToString();
                //txt_cmt.Text = result.Rows[0]["cmt"].ToString() + result.Rows[0]["gpkd"].ToString();
                if (result.Rows[0]["cmt"].ToString().Length == 0)
                    txt_cmt.Text = result.Rows[0]["cmt"].ToString();
                else
                    txt_cmt.Text = result.Rows[0]["gpkd"].ToString();
                txt_dienthoai.Text = result.Rows[0]["dienthoai"].ToString();
                txt_loaikh.Text = result.Rows[0]["loaikh"].ToString();
                txt_sodu.Text = String.Format("{0:#,##0.###}",result.Rows[0]["sodu"]);
                //textBox7.Text = result.Rows[0][7].ToString();
            }
        }
    }
}
